package com.hnzq.cloud.freshsendmessage.listener;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hnzq.cloud.OrderAPI;
import com.hnzq.cloud.dto.OrderDto;
import com.hnzq.cloud.freshsendmessage.param.CheckPayForOrderParam;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;


@RocketMQTransactionListener
@Slf4j
@Component
public class txTransactional implements RocketMQLocalTransactionListener {

    @Autowired
    private OrderAPI orderAPI;

    ObjectMapper mapper = new ObjectMapper();

    @SneakyThrows
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object obj) {

        log.info("执行本地事务");
        //拿到了传过来的对象
        CheckPayForOrderParam param = (CheckPayForOrderParam) obj;

        //根据orderId得到order
        OrderDto orderDto = mapper.convertValue(
                orderAPI.findOrderByOrderId(((CheckPayForOrderParam) obj).getOrderId()).getData()
                , new TypeReference<OrderDto>() {}
                );

        //模拟本地事务状态 ,0 成功 1失败 2未知
        switch (orderDto.getOrderStatus()){
            case -1 :
                log.info("事务已回滚");
                return RocketMQLocalTransactionState.ROLLBACK;
            case 0 :
                log.info("事务已回滚");
                return RocketMQLocalTransactionState.ROLLBACK;
            case 1:
                log.info("事务已提交");
                return RocketMQLocalTransactionState.COMMIT;

                default:
                    log.info("未知的事务");
                    return RocketMQLocalTransactionState.UNKNOWN;
        }
//        log.info("未知的事务");
//        return RocketMQLocalTransactionState.UNKNOWN;
    }

    @SneakyThrows
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {

        log.info("执行消息回查");

        CheckPayForOrderParam param = new CheckPayForOrderParam();

        try {
            //先将msg转换为json对象
            String jsonStr = new String((byte[]) msg.getPayload(), "utf-8");
            //再将json对象反序列化为java对象
            CheckPayForOrderParam checkPayForOrderParam = mapper.readValue(jsonStr, CheckPayForOrderParam.class);
            param = checkPayForOrderParam;

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //根据orderId得到order
        OrderDto orderDto = mapper.convertValue(
                orderAPI.findOrderByOrderId(param.getOrderId()).getData()
                , new TypeReference<OrderDto>() {}
        );


        //根据数据的状态决定走哪个方法
        switch (orderDto.getOrderStatus()){
            case -1 :
                log.info("回查后的回滚");
                return RocketMQLocalTransactionState.ROLLBACK;
            case 0 :
                log.info("回查后的回滚");
                return RocketMQLocalTransactionState.ROLLBACK;
            case 1:
                log.info("回查后的提交");
                return RocketMQLocalTransactionState.COMMIT;

            default:
                log.info("未知的事务");
                return RocketMQLocalTransactionState.UNKNOWN;
        }
//        return RocketMQLocalTransactionState.UNKNOWN;
    }
}

